import type { DefineComponent ,Component } from 'vue'

// 定义组件配置接口
interface ComponentConfig {
  type: string
  component: Component //DefineComponent<any, any, any>
  isChildComponent?: boolean
}

// 组件注册表
class ComponentRegistry {
  private components: Map<string, ComponentConfig> = new Map()
  private childComponents: Set<string> = new Set()

  // 注册单个组件
  register(config: ComponentConfig): void {
    this.components.set(config.type, config)
    if (config.isChildComponent) {
      this.childComponents.add(config.type)
    }
  }

  // 批量注册组件
  registerBatch(configs: ComponentConfig[]): void {
    configs.forEach((config) => this.register(config))
  }

  // 获取所有组件类型
  getAllComponentTypes(): string[] {
    return Array.from(this.components.keys()).filter((type) => !this.childComponents.has(type))
  }

  // 获取所有子组件类型
  getAllChildComponentTypes(): string[] {
    return Array.from(this.childComponents)
  }

  // 获取组件
  getComponent(type: string): DefineComponent<any, any, any> | undefined {
    return this.components.get(type)?.component as DefineComponent<any, any, any>
  }

  // 检查是否为子组件
  isChildComponent(type: string): boolean {
    return this.childComponents.has(type)
  }
}

// 导出单例实例
export const componentRegistry = new ComponentRegistry()

// 定义类型辅助函数，用于从注册表中提取类型
export type RegisteredComponentType = ReturnType<
  typeof componentRegistry.getAllComponentTypes
>[number]
export type RegisteredChildComponentType = ReturnType<
  typeof componentRegistry.getAllChildComponentTypes
>[number]
